package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Mapper
public interface OrderMapper {
    /**
     * 插入订单数据
     * @param order
     */
    void insert(Orders order);
    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber} and user_id=#{userId}")
    Orders getByNumberAndUserId(String orderNumber,Long userId);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 分页条件查询
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据id查询订单
     * @param id
     * @return
     */
    @Select("select * from orders where id=#{id}")
    Orders getById(Long id);

    /**
     * 根据状态统计订单数量
     * @param status
     */
    @Select("select count(id) from orders where status = #{status}")
    Integer countStatus(Integer status);

    /**
     * 根据时间范围查询营业额
     * @param begin
     * @param end
     * @return
     */
    @Select("select sum(amount) from orders where order_time between #{begin} and #{end} and status = 5")
    BigDecimal getTurnoverByTimeRange(LocalDateTime begin, LocalDateTime end);

    /**
     * 根据时间范围查询订单总数
     * @param begin
     * @param end
     * @return
     */
    @Select("select count(id) from orders where order_time between #{begin} and #{end}")
    Integer getOrderCountByTimeRange(LocalDateTime begin, LocalDateTime end);

    /**
     * 根据时间范围查询有效订单数
     * @param begin
     * @param end
     * @return
     */
    @Select("select count(id) from orders where order_time between #{begin} and #{end} and status = 5")
    Integer getValidOrderCountByTimeRange(LocalDateTime begin, LocalDateTime end);

}
